(function($){
    var total = 0;
    function initChecked() {
        var ids = paramTags.split(',');
        $('#bp_tags_layer li').each(function(){
            var obj = $(this);
            var id = obj.attr('did');
            for(var i=0; i<ids.length; i++) {
                if(id == ids[i]) {
                    obj.attr('c', '1');
                }
            }
        });
        totalChecked();
    }
    function checkedItem() {
        var obj = $(this);
        var checked = obj.attr('c');
        var v = checked == '1' ? '0' : '1';
        if(v >= '1' && total >= 3) {
            layer.msg('最多只能选择三个标签！', {icon: 2});
        } else {
            obj.attr('c', v);
            totalChecked();
        }
    }
    function setCheckedHtml(obj, checked) {
        if(checked) {
            total += 1;
            obj.addClass('checked');
            obj.find('i').removeClass('fa-square-o').addClass('fa-check-square-o');
        } else {
            obj.removeClass('checked');
            obj.find('i').removeClass('fa-check-square-o').addClass('fa-square-o');
        }
    }
    function totalChecked() {
        total = 0;
        $('#bp_tags_layer li').each(function(){
            var obj = $(this);
            var checked = obj.attr('c');
            setCheckedHtml(obj, checked == '1');
        });
    }
    function save() {
        var ids = [];
        var values = [];
        $('#bp_tags_layer li').each(function(){
            var obj = $(this);
            var checked = obj.attr('c');
            if(checked == '1') {
                ids.push(obj.attr('did'));
                values.push(obj.text());
            }
        });
        ids = ids.join(',');
        values = values.join(',');
        !!callback && eval(callback + "(\"" + ids + "\", \"" + values + "\")");
    }
    $(function(){
        initChecked();
        $('#bp_tags_layer li').on('click', checkedItem);
        $('#but_ok').on('click', save);
    });
})(jQuery);